using System.IO;
using Microsoft.Xna.Framework.Graphics;
using SerializationUtilities;


namespace Core.Graphics
{
    public enum TileSetTypes
    {
        Terrain,
        Characters,
        Items
    }

    public static class TileSetLoaderHelper
    {
        public static void LoadTileSets(string TileSetFolder, TileSetTypes TileSetType, GraphicsDevice GraphicsDevice)
        {
            //TileSet Collection to load into
            TileSetCollection tsc;
            //Get the TileSetFolder for this type
            GetTextureFolderAndCollection(ref TileSetFolder, TileSetType, out tsc);
            //Load the DirectoryInfo object
            DirectoryInfo diTexture = new DirectoryInfo(TileSetFolder);
            //Get All the file info's
            FileInfo[] files = diTexture.GetFiles("*.TileSet");
            //Utility for deserialization
            SerializationUtilities.Utilities<TileSet> Utilities = new SerializationUtilities.Utilities<TileSet>();
            //For each file
            foreach (FileInfo fi in files)
            {
                //Deserialize the TileSet and then add to collection
                TileSet ts = Utilities.ReadObjectFromFile(fi.OpenRead(), SerializationType.XML, CompressionType.SevenZip);
                ts.AfterLoadFromFileProcedure(GraphicsDevice);

                tsc.Add(ts.ID, ts);
                TileSetCollection.NextID = ts.ID;
            }
        }

        public static void SaveTileSet(TileSet ts, string TileSetFolder, string filename, TileSetTypes TileSetType)
        {
            //TileSet Collection to load into
            TileSetCollection tsc;
            //Get the TileSetFolder for this type
            GetTextureFolderAndCollection(ref TileSetFolder, TileSetType, out tsc);
            //Utility for deserialization
            SerializationUtilities.Utilities<TileSet> Utilities = new SerializationUtilities.Utilities<TileSet>();
            //Serialize the TileSet to File
            Utilities.WriteObjectToFile(ts, TileSetFolder + ts.Name + ".TileSet", SerializationType.XML, CompressionType.SevenZip);
        }

        private static void GetTextureFolderAndCollection(ref string TileSetFolder, TileSetTypes TileSetType, out TileSetCollection tsc)
        {
            switch (TileSetType)
            {
                case TileSetTypes.Terrain:
                    TileSetFolder += @"\Terrain\";
                    tsc = TileSetCollection.GlobalTerrainTileSets;
                    break;
                case TileSetTypes.Characters:
                    TileSetFolder += @"\Characters\";
                    tsc = TileSetCollection.GlobalCharacterTileSets;
                    break;
                case TileSetTypes.Items:
                    TileSetFolder += @"\Items\";
                    tsc = TileSetCollection.GlobalItemTileSets;
                    break;
                default:
                    tsc = new TileSetCollection();
                    break;
            }
        }

        public static TileSetCollection GetCollectionForType(TileSetTypes TileSetType)
        {
            switch (TileSetType)
            {
                case TileSetTypes.Terrain:
                    return TileSetCollection.GlobalTerrainTileSets;
                case TileSetTypes.Characters:
                    return TileSetCollection.GlobalCharacterTileSets;
                case TileSetTypes.Items:
                    return TileSetCollection.GlobalItemTileSets;
                default:
                    return new TileSetCollection();
            }
        }
    }
}
